package com.sinai.mshab.client.formatting;

import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.Widget;
import com.sinai.mshab.client.widgets.RadioCollection;

/**
 * This class takes in a widget and tells you if it's blank or not.
 * 
 * @author serge
 * 
 */
public class BlankChecker {

	/**
	 * Checks if the given widget is blank or not
	 * 
	 * @param widget
	 *            the widget to check
	 * @return true if the widget is checked, false otherwise.
	 */
	public static boolean isBlank(Widget widget) {

		// first check textboxes
		if (widget instanceof TextBoxBase) {
			String value = ((TextBoxBase) widget).getText();

			// if the value is null or has no length it's blank
			if (value == null || value.trim().length() == 0) {
				return true;
			}
		}
		// then check checkboxes.
		else if (widget instanceof CheckBox) {
			if (!((CheckBox) widget).isChecked()) {
				return true;
			}
		}
		// check radio collections
		else if (widget instanceof RadioCollection) {
			return !((RadioCollection)widget).isChecked();
		}
		
		return false;
	}
}
